package com.qf.ts.platform.front.web;

import com.qf.ts.platform.commons.pojo.R;
import com.qf.ts.platform.front.domain.Car;
import com.qf.ts.platform.front.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 对我的车辆管理进行接口提供
 */

@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;


    @PostMapping("/{uid}")
    public R addCar(@RequestBody Car car,@PathVariable("uid") Integer uid){

        int result = carService.addCar(car,uid);

        if(result > 0){

            return R.ok();
        }

        return R.error("fail");

    }

    @GetMapping("/{uid}")
    public List<Car> showCars(@PathVariable("uid") Integer uid){

        return carService.showCars(uid);

    }

    @GetMapping("/")
    public Car showCarDetail(Integer cid){

        return carService.showCarDetail(cid);

    }

    @DeleteMapping("/{cid}")
    public R deleteCar(@PathVariable("cid") Integer cid,HttpSession session){

        String  uid = (String)session.getAttribute("uid");

        int result = carService.deleteCar(uid,cid);

        if(result > 0){
            return R.ok("already deleted");
        }

        return R.error("fail");
    }

    @PutMapping("/")
    public R updateCar(@RequestBody Car car){

        Integer result = carService.updateCar(car);

        if(result > 0 ){
            return R.ok();
        }
        return R.error();

    }


}
